package com.example.bankend.controllers;

import com.example.bankend.entity.Card;
import com.example.bankend.entity.User;
import com.example.bankend.form.*;
import com.example.bankend.param.Result;
import com.example.bankend.service.CardService;
import com.example.bankend.service.UserService;
import com.example.bankend.util.Validator;
import com.example.bankend.vo.CardResult;
import com.example.bankend.vo.UserResult;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author luolinila
 * @date 2025/6/16 20:57
 */
@CrossOrigin
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/user/phone")
    public Result<Null> phone(@RequestParam String phone) {
        boolean inputTure = phone != null;
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        boolean isPhone = Validator.isValidPhoneNumber(phone);
        if (!isPhone) {
            return new Result<>(10004, "手机号格式错误", null);
        }
        int res = userService.sendCaptcha(phone);
        if (res == -1) {
            return new Result<>(10005, "发送失败", null);
        }
        return new Result<>(200, "发送成功", null);
    }

    @PostMapping("/user/create")
    public Result<UserResult> create(@RequestBody CreateUser form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        if ("admin".equals(form.getCustomerName())) {
            return new Result<>(10001, "用户名不能为admin", null);
        }
        boolean isPID = Validator.isValidIDCard(form.getPID());
        if (!isPID) {
            return new Result<>(10001, "身份证格式错误", null);
        }
        boolean isPhone = Validator.isValidPhoneNumber(form.getPhone());
        if (!isPhone) {
            return new Result<>(10002, "手机号格式错误", null);
        }
        boolean isPass = Validator.isValidPassword(form.getPass());
        if (!isPass) {
            return new Result<>(10003, "密码格式错误", null);
        }
        Long id = userService.create(form.getAdminID(), form.getAdminName(), form.getAdminPass(), form.getCustomerName(), form.getPID(), form.getPhone(), form.getPass());
        if (id == -1) {
            return new Result<>(10005, "注册失败", null);
        }
        User user = userService.getUserByCunstomerID(id);
        if (user == null) {
            return new Result<>(10006, "未知错误", null);
        }
        return new Result<>(200, "注册成功", new UserResult(user));
    }

    @PostMapping("/user/loginByPIDAndPass")
    public Result<UserResult> loginByPIDAndPass(@RequestBody LoginByPIDAndPass form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        boolean isPID = Validator.isValidIDCard(form.getPID());
        if (!isPID) {
            return new Result<>(10001, "身份证格式错误", null);
        }
        boolean isPass = Validator.isValidPassword(form.getPass());
        if (!isPass) {
            return new Result<>(10002, "密码格式错误", null);
        }
        Long id = userService.loginByPIDAndPass(form.getPID(), form.getPass());
        if (id == -1) {
            return new Result<>(10005, "密码错误", null);
        }
        User user = userService.getUserByCunstomerID(id);
        return new Result<>(200, "登录成功", new UserResult(user));
    }

    @PostMapping("/user/loginAdmin")
    public Result<UserResult> loginAdmin(@RequestBody LoginAdmid form) {
        boolean isTure = form.isTure();
        if (!isTure) {
            return new Result<>(10000, "输入错误", null);
        }
        User admin = userService.loginAdmin(form.getAdminID(), form.getAdminName(), form.getAdminPass());
        if (admin  == null) {
            return new Result<>(10005, "登录失败", null);
        }
        return new Result<>(200, "登录成功", new UserResult(admin));
    }

    @PostMapping("/user/delete")
    public Result deleteUser(@RequestBody DeleteUser form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        Long res = userService.deleteUser(form.getAdminID(), form.getAdminName(), form.getAdminPass(), form.getCustomerID());
        if(res == -1) {
            return new Result<>(10002, "注销失败", null);
        }
        if (res == 10003) {
            return new Result(10003, "客户不存在", null);
        }
        return new Result<>(200, "注销成功", null);
    }

    @PostMapping("/user/update")
    public Result<UserResult> updateUser(@RequestBody UpadteUserInfo form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        boolean isPass = Validator.isValidPassword(form.getPass());
        if (!isPass) {
            return new Result<>(10003, "密码格式错误", null);
        }
        boolean isPhone = Validator.isValidPhoneNumber(form.getTelephone());
        if (!isPhone) {
            return new Result<>(10004, "手机号格式错误", null);
        }
        Long res = userService.updateUser(form.getAdminID(), form.getAdminName(), form.getAdminPass(), form.getCustomerID(), form.getPass(), form.getTelephone(), form.getAddress());
        if (res == -1) {
            return new Result<>(10004, "修改失败", null);
        }
        if (res == 10001) {
            return new Result<>(10005, "客户不存在", null);
        }
        User user = userService.getUserByCunstomerID(res);
        if (user == null) {
            return new Result<>(10011, "未知错误", null);
        }
        return new Result<>(200, "修改成功", new UserResult(user));
    }

    @PostMapping("/user/selectByID")
    public Result<UserResult> selectByID(@RequestBody SelectByID form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        Long res = userService.selectByID(form.getAdminID(), form.getAdminName(), form.getAdminPass(), form.getCustomerID());
        if (res == -1) {
            return new Result<>(10005, "查询失败", null);
        }
        if (res == 10001) {
            return new Result<>(10006, "客户不存在", null);
        }
        User user = userService.getUserByCunstomerID(res);
        if (user == null) {
            return new Result<>(10008, "未知错误", null);
        }
        return new Result<>(200, "查询成功", new UserResult(user));
    }

    @PostMapping("/user/selectLikeByName")
    public Result<List<UserResult>> selectLikeByName(@RequestBody SelectLikeByName form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        List<User> users = userService.selectLikeByName(form.getAdminID(), form.getAdminName(), form.getAdminPass(), form.getCustomerName());
        if (users == null) {
            return new Result<>(10005, "查询失败", null);
        }
        List<UserResult> resList = new ArrayList<>();
        for (User user : users) {
            resList.add(new UserResult(user));
        }
        return new Result<>(200, "查询成功", resList);
    }

    @PostMapping("/user/selectByPID")
    public Result<UserResult> selectByPID(@RequestBody SelectByPID form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        boolean isPID = Validator.isValidIDCard(form.getPID());
        if (!isPID) {
            return new Result<>(10001, "身份证格式错误", null);
        }
        Long res = userService.selectByPID(form.getAdminID(), form.getAdminName(), form.getAdminPass(), form.getPID());
        if (res == -1) {
            return new Result<>(10002, "查询失败", null);
        }
        if (res == 10001) {
            return new Result<>(10003, "客户不存在", null);
        }
        User user = userService.getUserByCunstomerID(res);
        if (user == null) {
            return new Result<>(10004, "未知错误", null);
        }
        return new Result<>(200, "查询成功", new UserResult(user));
    }

    @PostMapping("/user/selectAll")
    public Result<List<UserResult>> selectAll(@RequestBody SelectAll form) {
        boolean inputTure = form.isTure();
        if (!inputTure) {
            return new Result<>(10000, "输入错误", null);
        }
        List<User> users = userService.selectAll(form.getAdminID(), form.getAdminName(), form.getAdminPass());
        if (users == null) {
            return new Result<>(10001, "未知错误", null);
        }
        List<UserResult> resUsers =  new ArrayList<>();
        for (User user : users) {
            resUsers.add(new UserResult(user));
        }
        return new Result<>(200, "获取成功", resUsers);
    }
}
